package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.CommentComstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.model.mess.ArticleVisitStreamMess;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleIncrHandleListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void onMessage(String message){
        if(StringUtils.isNotBlank(message)){
            System.out.println("最终聚合的数据为："+message);
            apArticleService.updateScore(JSON.parseObject(message, ArticleVisitStreamMess.class));
        }
    }

    /**
     * 更新文章的评论
     *
     */
    @KafkaListener(topics = CommentComstants.ARTICLE_COMMENT_TOPIC)
    private void updateArticleComment(String mess){
        if(StringUtils.isNotBlank(mess)){
            apArticleService.updaeComment(mess);
        }

    }

}
